package com.zxy.leetcode.lcci;

import java.util.Arrays;

/**
 * https://leetcode-cn.com/problems/check-permutation-lcci/
 *
 * 判定是否互为字符重排
 * 给定两个字符串 s1 和 s2，请编写一个程序，
 * 确定其中一个字符串的字符重新排列后，能否变成另一个字符串。
 */
public class Test0102 {

    public static void main(String[] args) {
        Test0102 test = new Test0102();
        System.out.println(test.CheckPermutation("abc", "bca"));
        System.out.println(test.CheckPermutation("abc", "bcd"));
    }

    public boolean CheckPermutation(String s1, String s2) {
        if (s1.length() == 0 && s2.length() == 0) {
            return true;
        }
        if (s1.length() != s2.length()) {
            return false;
        }

        char[] chars1 = s1.toCharArray();
        char[] chars2 = s2.toCharArray();

        Arrays.sort(chars1);
        Arrays.sort(chars2);

        for (int i=0; i<chars1.length; i++) {
            if (chars1[i] != chars2[i]) {
                return false;
            }
        }

        return true;
    }
}
